// API响应基础接口
export interface ApiResponse<T = any> {
  success: boolean
  message: string
  data: T
  error?: {
    code: string
    message: string
    details?: any
    stack?: string
    suggestions?: string[]
  }
  timestamp: string
  requestId?: string
}

// 分页响应接口
export interface PaginatedResponse<T = any> extends ApiResponse<T[]> {
  pagination: {
    page: number
    pageSize: number
    total: number
    totalPages: number
    hasNext: boolean
    hasPrev: boolean
  }
}

// 批量操作响应接口
export interface BatchOperationResponse {
  success: boolean
  total: number
  successful: number
  failed: number
  errors: Array<{
    id: string
    error: string
  }>
}

// 请求参数基础接口
export interface BaseParams {
  page?: number
  pageSize?: number
  sortBy?: string
  sortOrder?: 'asc' | 'desc'
}

// 搜索参数接口
export interface SearchParams extends BaseParams {
  keyword?: string
  filters?: Record<string, any>
  dateRange?: {
    start: string
    end: string
  }
}

// 错误码枚举
export enum ApiErrorCode {
  // 通用错误
  UNKNOWN_ERROR = 'UNKNOWN_ERROR',
  VALIDATION_ERROR = 'VALIDATION_ERROR',
  AUTHENTICATION_ERROR = 'AUTHENTICATION_ERROR',
  AUTHORIZATION_ERROR = 'AUTHORIZATION_ERROR',
  NOT_FOUND = 'NOT_FOUND',
  CONFLICT = 'CONFLICT',
  RATE_LIMIT_EXCEEDED = 'RATE_LIMIT_EXCEEDED',
  
  // 数据库相关错误
  DATABASE_CONNECTION_ERROR = 'DATABASE_CONNECTION_ERROR',
  DATABASE_QUERY_ERROR = 'DATABASE_QUERY_ERROR',
  DATABASE_CONSTRAINT_ERROR = 'DATABASE_CONSTRAINT_ERROR',
  DATABASE_TIMEOUT = 'DATABASE_TIMEOUT',
  
  // 脚本相关错误
  SCRIPT_NOT_FOUND = 'SCRIPT_NOT_FOUND',
  SCRIPT_SYNTAX_ERROR = 'SCRIPT_SYNTAX_ERROR',
  SCRIPT_EXECUTION_ERROR = 'SCRIPT_EXECUTION_ERROR',
  SCRIPT_PERMISSION_DENIED = 'SCRIPT_PERMISSION_DENIED',
  SCRIPT_VERSION_CONFLICT = 'SCRIPT_VERSION_CONFLICT',
  
  // 连接相关错误
  CONNECTION_NOT_FOUND = 'CONNECTION_NOT_FOUND',
  CONNECTION_TEST_FAILED = 'CONNECTION_TEST_FAILED',
  CONNECTION_TIMEOUT = 'CONNECTION_TIMEOUT',
  CONNECTION_AUTHENTICATION_FAILED = 'CONNECTION_AUTHENTICATION_FAILED',
  CONNECTION_POOL_EXHAUSTED = 'CONNECTION_POOL_EXHAUSTED',
  
  // 执行相关错误
  EXECUTION_NOT_FOUND = 'EXECUTION_NOT_FOUND',
  EXECUTION_ALREADY_RUNNING = 'EXECUTION_ALREADY_RUNNING',
  EXECUTION_CANCELLED = 'EXECUTION_CANCELLED',
  EXECUTION_TIMEOUT = 'EXECUTION_TIMEOUT',
  EXECUTION_QUEUE_FULL = 'EXECUTION_QUEUE_FULL',
}

// HTTP状态码映射
export const HttpStatusMap: Record<ApiErrorCode, number> = {
  [ApiErrorCode.UNKNOWN_ERROR]: 500,
  [ApiErrorCode.VALIDATION_ERROR]: 400,
  [ApiErrorCode.AUTHENTICATION_ERROR]: 401,
  [ApiErrorCode.AUTHORIZATION_ERROR]: 403,
  [ApiErrorCode.NOT_FOUND]: 404,
  [ApiErrorCode.CONFLICT]: 409,
  [ApiErrorCode.RATE_LIMIT_EXCEEDED]: 429,
  
  [ApiErrorCode.DATABASE_CONNECTION_ERROR]: 503,
  [ApiErrorCode.DATABASE_QUERY_ERROR]: 500,
  [ApiErrorCode.DATABASE_CONSTRAINT_ERROR]: 400,
  [ApiErrorCode.DATABASE_TIMEOUT]: 504,
  
  [ApiErrorCode.SCRIPT_NOT_FOUND]: 404,
  [ApiErrorCode.SCRIPT_SYNTAX_ERROR]: 400,
  [ApiErrorCode.SCRIPT_EXECUTION_ERROR]: 500,
  [ApiErrorCode.SCRIPT_PERMISSION_DENIED]: 403,
  [ApiErrorCode.SCRIPT_VERSION_CONFLICT]: 409,
  
  [ApiErrorCode.CONNECTION_NOT_FOUND]: 404,
  [ApiErrorCode.CONNECTION_TEST_FAILED]: 400,
  [ApiErrorCode.CONNECTION_TIMEOUT]: 504,
  [ApiErrorCode.CONNECTION_AUTHENTICATION_FAILED]: 401,
  [ApiErrorCode.CONNECTION_POOL_EXHAUSTED]: 503,
  
  [ApiErrorCode.EXECUTION_NOT_FOUND]: 404,
  [ApiErrorCode.EXECUTION_ALREADY_RUNNING]: 409,
  [ApiErrorCode.EXECUTION_CANCELLED]: 200,
  [ApiErrorCode.EXECUTION_TIMEOUT]: 504,
  [ApiErrorCode.EXECUTION_QUEUE_FULL]: 503,
}

// API请求配置接口
export interface ApiRequestConfig {
  timeout?: number
  retries?: number
  retryDelay?: number
  headers?: Record<string, string>
}

// 文件上传响应接口
export interface FileUploadResponse {
  filename: string
  originalName: string
  size: number
  mimetype: string
  url: string
  uploadedAt: string
}

// 健康检查响应接口
export interface HealthCheckResponse {
  status: 'healthy' | 'unhealthy' | 'degraded'
  timestamp: string
  uptime: number
  version: string
  services: {
    database: ServiceStatus
    redis: ServiceStatus
    doris?: ServiceStatus
  }
}

export interface ServiceStatus {
  status: 'up' | 'down' | 'degraded'
  responseTime?: number
  error?: string
  lastCheck: string
}